package com.xc.hotelmgr.order.modules.ctrip.sdk;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import com.xc.hotelmgr.order.common.utils.DateUtils;
import com.xc.hotelmgr.order.modules.order.entity.DtOrderChannelsEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.UUID;

@Slf4j
public class CtripClient {

    private static String accessTokenUrl = "http://openservice.ctrip.com/openserviceauth/authorize.ashx?AID=%s&SID=%d&KEY=%s";
    private static String refreshTokenUrl = "http://openservice.ctrip.com/openserviceauth/refresh.ashx?AID=%s&SID=%d&refresh_token=%s";
    private static String url = "http://openservice.ctrip.com/openservice/serviceproxy.ashx?AID=%d&SID=%d&ICODE=%s&UUID=%s&Token=%s&mode=1&format=json";

    /**
     * 获取token
     * @param dtOrderChannelsEntity
     */
    public  static void getToken(DtOrderChannelsEntity dtOrderChannelsEntity){
        String newUrl = String.format(accessTokenUrl,dtOrderChannelsEntity.getAccessKey(), dtOrderChannelsEntity.getSid(),dtOrderChannelsEntity.getSecretKey());
        String resp = HttpUtil.get(newUrl);
        JSONObject respObj  = JSONObject.parseObject(resp);
        if(respObj != null && StringUtils.isNotBlank(respObj.getString("Access_Token"))) {
            dtOrderChannelsEntity.setAccessToken(respObj.getString("Access_Token"));
            dtOrderChannelsEntity.setRefreshToken(respObj.getString("Refresh_Token"));
            dtOrderChannelsEntity.setExpiresIn(respObj.getInteger("Expires_In"));
            dtOrderChannelsEntity.setRefreshTime(DateUtils.addDateSeconds(new Date(),dtOrderChannelsEntity.getExpiresIn()-3));
        }
    }

    /**
     * 更新token
     * @param dtOrderChannelsEntity
     */
    public static void refreshToken(DtOrderChannelsEntity dtOrderChannelsEntity) {
        String newUrl = String.format(refreshTokenUrl,dtOrderChannelsEntity.getAccessKey(), dtOrderChannelsEntity.getSid(),dtOrderChannelsEntity.getRefreshToken());
        String resp = HttpUtil.get(newUrl);
        //如果token无效，从新获取token
        if(resp.contains("INVALID_REFRESHTOKEN")){
            getToken(dtOrderChannelsEntity);
            System.out.println("==getToken==");
            return;
        }
        JSONObject respObj  = JSONObject.parseObject(resp);
        if(respObj != null && StringUtils.isNotBlank(respObj.getString("Access_Token"))) {
            dtOrderChannelsEntity.setAccessToken(respObj.getString("Access_Token"));
            dtOrderChannelsEntity.setRefreshToken(respObj.getString("Refresh_Token"));
            dtOrderChannelsEntity.setExpiresIn(respObj.getInteger("Expires_In"));
            dtOrderChannelsEntity.setRefreshTime(DateUtils.addDateSeconds(new Date(),dtOrderChannelsEntity.getExpiresIn()-3));
        }
        System.out.println("==refreshToken==" + respObj);
    }

    /**
     * 携程数据请求
     * @param ctripDtInfo
     * @return
     */
    public static String excuteReq(CtripDtInfo ctripDtInfo) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String newUrl = String.format(url, ctripDtInfo.getAID(), ctripDtInfo.getSID(), ctripDtInfo.getICODE(), uuid, ctripDtInfo.getAccess_Token());
        return HttpRequest.post(newUrl).body(String.valueOf(ctripDtInfo.getJsonData())).execute().body();
    }
}
